import 'dart:async';
import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_style.dart';
import 'dart:ui' as ui;

class LButton extends StatelessWidget {
  String text;
  VoidCallback click;
  Color? highlightColor;
  LStyle lStyle;
  late Widget body;

  LButton(this.text, this.click,
      {super.key, this.highlightColor, required this.lStyle}) {
    body = Container(
      width: lStyle.width,
      height: lStyle.height,
      padding: lStyle.padding,
      margin: lStyle.margin,
      child: MaterialButton(
        height: lStyle.height,
        padding: EdgeInsets.zero,
        color: lStyle.backgroundColor,
        elevation: 0,
        highlightColor: highlightColor,
        colorBrightness: Brightness.dark,
        splashColor: Colors.grey,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(lStyle.radius ?? 5)),
        onPressed: click,
        child: Text(
          text,
          style: TextStyle(fontSize: lStyle.fontSize,
              color: lStyle.color,
              fontWeight: lStyle.fontWeight),
        ),
      ),
    );
    if (lStyle.flex != null) {
      body = Expanded(
        flex: lStyle.flex!,
        child: body,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return body;
  }
}

class OldLImage extends StatelessWidget {
  VoidCallback? onTap;
  BoxFit? fit;
  late Widget body;

  static OldLImage easy(String path,
      [double? height, double? width, double? radius]) =>
      OldLImage(
        path,
        lStyle: LStyle(height: height, width: width, radius: radius),
      );

  OldLImage(str, {super.key, this.fit, this.onTap, required LStyle lStyle}) {
    String path = str ?? "";
    // if (path.contains("/Image/") && !path.contains("http")) {
    //   path = Api.baseUrlDevelop + path;
    // }
    if (path.contains("http")) {
      // return DisposableCachedImage.network(imageUrl: path.trim());
      body = CachedNetworkImage(
        fit: fit ?? BoxFit.cover,
        fadeInDuration: const Duration(milliseconds: 300),
        fadeOutDuration: const Duration(milliseconds: 0),
        imageUrl: path.trim(),
        width: lStyle.width,
        height: lStyle.height,
        // placeholder: (context, url) =>
        //     Image.asset(loadingbg??bg_default_loading),
        // errorWidget: (context, url, error) =>
        //     Image.asset(loadingbg??bg_default_loading),
      );
      // } else if (path.contains(".svg")) {
      //   return SvgPicture.asset(path);
    } else if (path.contains("assets")) {
      body = Image.asset(path.trim(), fit: fit ?? BoxFit.cover,
          width: lStyle.width,
          height: lStyle.height,
          gaplessPlayback: true);
    } else {
      body = Image.file(
        File(path.trim()),
        fit: fit ?? BoxFit.cover,
        width: lStyle.width,
        height: lStyle.height,
      );
    }

    body = Container(
      width: lStyle.width,
      height: lStyle.height,
      padding: lStyle.padding,
      margin: lStyle.margin,
      child: body,
    );
    if (lStyle.radius != null && lStyle.radius != 0) {
      body = ClipRRect(
        borderRadius: BorderRadius.circular(lStyle.radius!),
        child: body,
      );
    }
    if (lStyle.borderRadius != null) {
      body = ClipRRect(
        borderRadius: lStyle.borderRadius!,
        child: body,
      );
    }
    if (onTap != null) {
      body = InkWell(
        child: body,
        onTap: onTap,
      );
    }
    if (lStyle.flex != null) {
      body = Expanded(
        flex: lStyle.flex!,
        child: body,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return body;
  }
}

class LImage extends StatelessWidget with LWComm {
  VoidCallback? onTap;
  BoxFit? fit;
  late Widget body;
  String? path;

  static LImage e(String? path,
      [double? width, double? height, double? radius]) =>
      LImage.easy(path, height, width, radius);

  static LImage easy(String? path,
      [double? height, double? width, double? radius]) =>
      LImage(
        path ?? "",
        lStyle: LStyle(height: height, width: width, radius: radius),
      );

  LImage(str, {super.key, this.fit, this.onTap, LStyle? lStyle}) {
    path = str;
    if (lStyle != null) {
      this.lStyle = lStyle;
    }
    if (onTap != null) {
      this.lStyle.onTap = onTap;
    }
  }

  @override
  Widget build(BuildContext context) {
    return OldLImage(path, lStyle: lStyle, onTap: lStyle.onTap);
  }
}
